package com.youmingxing.network;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.youmingxing.AllStarApp;
import com.youmingxing.utils.SharePreferenceUtil;
import com.youmingxing.utils.StringUtil;

import org.json.JSONArray;
import org.json.JSONException;

public class AllStarApi {
    private static String MOVIE_LIST = "movie/list.json";
    private static String UPDATES_LIST = "updates/list.json";
    private static String UPDATES_MYCARE = "updates/mycare.json";
    private static String POST_NEW_CONTENT = "updates/add.action";
    private static String AUDITION_DETAIL = "audition/cate/detail.json";
    private static String USER_PROFILE = "user/profile/show.json";
    private static String GET_USER_CONTENTS = "updates/user_list.json";
    private static String GET_LATEST_COMMENTS = "updates/comments.json";
    private static String GET_HOT_COMMENTS = "updates/hot_comments.json";
    private static String THUMBSUP_COMMENT = "updates/comment_thumbsup.action";
    private static String COMMENT = "updates/comment.action";

<<<<<<< Updated upstream
    private static String NEWSCOUNT = "updates/pullnew.json";

    private static String IDENTIFY = "init.json";

=======
>>>>>>> Stashed changes
    //获取电影列表
    public static void getMovieList(JsonHttpResponseHandler handler) {
        HttpManager.client.post(HttpManager.BASE_URL + MOVIE_LIST, HttpManager.getBaseParams(), handler);
    }

    //获取优秀作品
    public static void getAuditionInfo(int movieID, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("movie_id", movieID);
        HttpManager.client.post(HttpManager.BASE_URL + "movie/audition_info_v2_1.json", params, handler);
    }

    public static void getGreateContent(String url, long remark, int id, JsonHttpResponseHandler
            handler) {
        RequestParams params = HttpManager.getBaseParams();
        if (remark != -1) {
            params.put("remark", remark);
        }
        if (id != -1) {
            params.put("id", id);
        }
        HttpManager.client.post(url, params, handler);
    }

    public static void getContents(int type, long remark, int id, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("show", type);
        params.put("remark", remark);
        params.put("id", id);
        HttpManager.client.post(HttpManager.BASE_URL + UPDATES_LIST, params, handler);
    }

    public static void getFocus(long remark, int id, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("remark", remark);
        params.put("id", id);
        HttpManager.client.post(HttpManager.BASE_URL + UPDATES_MYCARE, params, handler);
    }

    public static void postNewContent(int type, String title, String url, String thumbnail, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("type", type);
        params.put("title", title);
        params.put("url", url);
        params.put("thumbnail", thumbnail);
        HttpManager.client.post(HttpManager.BASE_URL + POST_NEW_CONTENT, params, handler);
    }

    public static void getAuditionDetail(int movieID, int cateID, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("movie_id", movieID);
        params.put("cate_id", cateID);
        HttpManager.client.post(HttpManager.BASE_URL + AUDITION_DETAIL, params, handler);
    }

    public static void getUserProfile(int userid, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("uid", userid);
        HttpManager.client.post(HttpManager.BASE_URL + USER_PROFILE, params, handler);
    }

    //海选排名
    public static void getAuditionRank(int movieId, int cateId, int mark, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("movie_id", movieId);
        params.put("cate_id", cateId);
        params.put("mark", mark);
        HttpManager.client.post(HttpManager.BASE_URL + "audition/rank.json", params, handler);
    }

    //获取某人的作品
    public static void getUserContents(int uid, int mark, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("uid", uid);
        if (mark != 0) {
            params.put("mark", mark);
        }
        HttpManager.client.post(HttpManager.BASE_URL + GET_USER_CONTENTS, params, handler);
    }

    //获取作品的评论最新
    public static void getLatestComments(int uid, long mark, long cid, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("id", uid);
        if (mark != -1) {
            params.put("mark", mark);
        }
        if (cid != -1) {
            params.put("cid", cid);
        }
        HttpManager.client.post(HttpManager.BASE_URL + GET_LATEST_COMMENTS, params, handler);
    }

    //获取作品的评论热门
    public static void getGetHotComments(int id, int mark, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("id", id);
        params.put("mark", mark);
        HttpManager.client.post(HttpManager.BASE_URL + GET_HOT_COMMENTS, params, handler);
    }

    //给评论点赞
    public static void thumbsUpComment(long id, int contentId, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("id", id);
        params.put("update_id", contentId);
        HttpManager.client.post(HttpManager.BASE_URL + THUMBSUP_COMMENT, params, handler);
    }

    //对作品点赞
    public static void thumsUpContent(int id, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("id", id);
        HttpManager.client.post(HttpManager.BASE_URL + "updates/thumbsup.action", params, handler);
    }

    //评论某作品
    public static void comment(String comment, int contentId, int to_uid, JsonHttpResponseHandler
            handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("content", comment);
        params.put("id", contentId);
        params.put("to_uid", to_uid); //如果该评论指定回复某人，传被回复者用户ID。未指定回复人 传-1或0或不传。
        HttpManager.client.post(HttpManager.BASE_URL + COMMENT, params, handler);
    }

    //点击我要报名 对用户报名海选资格进行判断
    public static void checkQualification(int movieId, int cateId, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("movie_id", movieId);
        params.put("cate_id", cateId);
        HttpManager.client.post(HttpManager.BASE_URL + "audition/check_qualification.json", params, handler);
    }

    //报名个人资料确认 是否都填写完整
    public static void checkUserInfo(JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        HttpManager.client.post(HttpManager.BASE_URL + "audition/check_userinfo.json", params, handler);
    }

    //报名海选
    public static void applyAudition(int movieId, int cateId, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("movie_id", movieId);
        params.put("cate_id", cateId);
        HttpManager.client.post(HttpManager.BASE_URL + "audition/apply.action", params, handler);
    }

    //我参加的海选,movieId=-1时,为不传该参数
    public static void getMyAudition(int movieId, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        if (movieId != -1) {
            params.put("movie_id", movieId);
        }
        HttpManager.client.post(HttpManager.BASE_URL + "audition/detail.json", params, handler);
    }

    //关注or取消关注
    public static void focus(int to, int action, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("to", to);
        params.put("action", action); //1:关注; 0:取消关注
        HttpManager.client.post(HttpManager.BASE_URL + "user/follow/change.action", params, handler);
    }

    //某用户参加的海选,movieId=-1时,为不传该参数
    public static void getUserAudition(int userId, int movieId, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("u_id", userId);
        if (movieId != -1) {
            params.put("movie_id", movieId);
        }
        HttpManager.client.post(HttpManager.BASE_URL + "audition/user_detail.json", params, handler);
    }

    //根据海选id查询海选详情
    public static void showAudition(int id, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("id", id);
        HttpManager.client.post(HttpManager.BASE_URL + "audition/show.json", params, handler);
    }

    //退出海选
    public static void quitAudition(int id, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("id", id);
        HttpManager.client.post(HttpManager.BASE_URL + "audition/quit.action", params, handler);
    }

    //给用户海选加油
    public static void vote(int id, int num, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("id", id);
        params.put("num", num);
        HttpManager.client.post(HttpManager.BASE_URL + "audition/vote.action", params, handler);
    }

    //搜索用户接口 支持通过中文、拼音、拼音缩写搜索用户昵称
    public static void searchUser(String keyword, int mark, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("kw", keyword);
        params.put("mark", mark);
        HttpManager.client.post(HttpManager.BASE_URL + "user/search.json", params, handler);
    }

    //搜索用户接口和作品 支持通过中文、拼音、拼音缩写搜索用户昵称和作品
    public static void searchUserAndWork(String keyword, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("kw", keyword);
        HttpManager.client.post(HttpManager.BASE_URL + "search/all.json", params, handler);
    }

    //搜索中查看更多用户
    public static void searchMoreUser(String keyword, int mark, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("kw", keyword);
        params.put("mark", mark);
        HttpManager.client.post(HttpManager.BASE_URL + "search/user.json", params, handler);
    }

    //搜索中查看更多作品
    public static void searchMoreWork(String keyword, int mark, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("kw", keyword);
        params.put("mark", mark);
        HttpManager.client.post(HttpManager.BASE_URL + "search/works.json", params, handler);
    }


    //通过兑换码兑换卡券
    public static void searchCoupons(String code, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("code", code);
        HttpManager.client.post(HttpManager.BASE_URL + "card/activate.action", params, handler);
    }

    //显示自己的活动券
    public static void showCoupons(JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        HttpManager.client.post(HttpManager.BASE_URL + "card/list.json",params,handler);
    }

    // 获取置顶券的置顶日期
    public static void getCouponsDate(String cardno, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("cardno", cardno);
        HttpManager.client.post(HttpManager.BASE_URL + "card/topset_list.json", params, handler);
    }

    // 获取置顶位时段
    public static void getCouponsTime(String cardno,String item_id,JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("cardno", cardno);
        params.put("item_id", item_id);
        HttpManager.client.post(HttpManager.BASE_URL + "card/topset_list_item.json", params, handler);
    }

    //获取作品详情
    public static void getContentDetail(int id, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("id", id);
        HttpManager.client.post(HttpManager.BASE_URL + "updates/detail.json", params, handler);
    }

    //给某个用户投票者排行榜
    public static void getUserVoters(int id, int mark, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("uid", id);
        params.put("mark", mark);
        HttpManager.client.post(HttpManager.BASE_URL + "ballot/user_voter.json", params, handler);
    }

    //获取#活动列表
    public static void getTopicEvents(JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        HttpManager.client.post(HttpManager.BASE_URL + "activity/topics.json",params,handler);
    }


    //APP启动检测信息接口
    public static void getUpdateInfo(JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        HttpManager.client.post(HttpManager.BASE_URL + "init.json", params, handler);
    }

    //2.1版本电影类目
    public static void getCategories(int movie_id, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("movie_id", movie_id);
        HttpManager.client.post(HttpManager.BASE_URL + "movie/categories.json", params, handler);
    }

    //banner运营位
    public static void getBanner(int position, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("position", position);
        HttpManager.client.post(HttpManager.BASE_URL + "activity/operation.json", params, handler);
    }


    //新消息数量
    public static void getNewsCount(int show, long lasttime, JsonHttpResponseHandler handler) {
        RequestParams params = HttpManager.getBaseParams();
        params.put("show", show);
        params.put("lasttime", lasttime);
        HttpManager.client.post(HttpManager.BASE_URL + NEWSCOUNT, params, handler);
    }


    public static String getUserMarkIcon(int type) {
        String iconUrl = null;
        String usermark = null;
        if (StringUtil.isEmpty(usermark)) {
            usermark = getUsermarkJson();
        }
        JSONArray array = null;
        try {
            array = new JSONArray(usermark);
//            LogUtil.i("------- array.length()----","" +  array.length() + " "
//                    + array.optJSONObject(1).optInt("user_type"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < array.length(); i++) {
            if (type == array.optJSONObject(i).optInt("user_type")) {
                iconUrl = array.optJSONObject(i).optString("url");
                break;
            }
        }
        return iconUrl;
    }

    private static String getUsermarkJson() {
        SharePreferenceUtil sp = new SharePreferenceUtil(AllStarApp.getInstance());
        return sp.getUsermark();
    }
}